package com.tianhui.consignortwofei.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.fgs.common.imageView.strategy.ImageLoader;
import com.fgs.common.utils.ToastUtil;
import com.tianhui.consignortwofei.R;

/**
 * @author fgs
 * @date 2020/11/10
 * @description
 **/
public class AuthView extends LinearLayout {
    private int imageId;
    private int statusImageId;
    private String title;
    private String tip;
    private int imageHeight;
    private int imageWidth;
    private boolean isClickable;
    private ImageView imageView;
    private ImageView statusImageView;
    private TextView titleTextView;
    private TextView tipTextView;
    private View tipView;
    private FrameLayout frameLayout;
    private OnAuthPictureViewClickListener onAuthPictureViewClickListener;

    public AuthView(Context context) {
        this(context, null);
    }

    public AuthView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AuthView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AuthView);
        try {
            imageId = typedArray.getResourceId(R.styleable.AuthView_AuthViewImage, R.mipmap.icon_take_picture);
            imageHeight = typedArray.getLayoutDimension(R.styleable.AuthView_AuthViewImageHeight, -2);
            imageWidth = typedArray.getLayoutDimension(R.styleable.AuthView_AuthViewImageWidth, -2);
            statusImageId = typedArray.getResourceId(R.styleable.AuthView_AuthViewStatusImage, 0);
            title = typedArray.getString(R.styleable.AuthView_AuthViewTitle);
            tip = typedArray.getString(R.styleable.AuthView_AuthViewTip);
            isClickable = typedArray.getBoolean(R.styleable.AuthView_AuthViewIsClickable, true);

        } finally {
            typedArray.recycle();
        }

        View view = LayoutInflater.from(context).inflate(R.layout.layout_auth_view, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(layoutParams);
        addView(view);

        imageView = view.findViewById(R.id.layout_auth_view_imageView);
        statusImageView = view.findViewById(R.id.layout_auth_view_statusImageView);
        titleTextView = view.findViewById(R.id.layout_auth_view_titleTextView);
        tipTextView = view.findViewById(R.id.layout_auth_view_tipTextView);
        tipView = view.findViewById(R.id.layout_auth_view_tipView);
        frameLayout = view.findViewById(R.id.layout_auth_view_frameLayout);

        setImage(imageId);
        setStatusImage(statusImageId);
        setTitle(title);
        setTip(tip);
        setIsClickable(isClickable);
        setImageSize(imageHeight, imageWidth);

        initListener();

    }

    public void setImageSize(float imageHeight, float imageWidth) {
        ViewGroup.LayoutParams layoutParams;
        layoutParams = imageView.getLayoutParams();
        if (imageHeight == -1) {
            layoutParams.height = LayoutParams.MATCH_PARENT;
        } else if (imageHeight == -2) {
            layoutParams.height = LayoutParams.WRAP_CONTENT;
        } else {
            layoutParams.height = (int) imageHeight;
        }

        if (imageWidth == -1) {
            layoutParams.width = LayoutParams.MATCH_PARENT;
        } else if (imageWidth == -2) {
            layoutParams.width = LayoutParams.WRAP_CONTENT;
        } else {
            layoutParams.width = (int) imageWidth;
        }

        imageView.setLayoutParams(layoutParams);
    }

    public void setStatusImage(int statusImageId) {
        if (statusImageId != 0 && statusImageView != null) {
            statusImageView.setImageResource(statusImageId);
        }
    }

    private void initListener() {
        frameLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isClickable) {
                    if (onAuthPictureViewClickListener != null)
                        onAuthPictureViewClickListener.onPictureViewClick(v);
                } else {
                    ToastUtil.showShortToast("审核已通过，您不可以更改信息，若需要更改，请联系客服！");
                }

            }
        });
    }

    public void setOnAuthPictureViewClickListener(OnAuthPictureViewClickListener onAuthPictureViewClickListener) {
        this.onAuthPictureViewClickListener = onAuthPictureViewClickListener;

    }

    public void setIsClickable(boolean isClickable) {
        this.isClickable = isClickable;
//        if (frameLayout != null) {
//            frameLayout.setClickable(isClickable);
//        }
    }

    public void setTip(String titleText) {
        if (tipTextView != null) {
            if (!TextUtils.isEmpty(titleText)) {
                tipTextView.setText(titleText);
            }
        }
    }

    public void setTitle(String title) {
        if (titleTextView != null) {
            if (!TextUtils.isEmpty(title)) {
                tipView.setVisibility(View.VISIBLE);
            } else {
                tipView.setVisibility(View.GONE);
            }
            titleTextView.setText(title);
        }
    }

    public void setImage(int imageId) {
        if (imageView != null) {
            imageView.setImageResource(imageId);
        }
    }

    public void setImageWithPath(String path) {
        if (!TextUtils.isEmpty(path) && imageView != null)
            ImageLoader.init().loadImageWithUrl(imageView, path, false);
    }

    public void setImageWithId(String imageId) {
        if (!TextUtils.isEmpty(imageId) && imageView != null)
            ImageLoader.init().loadImageWithId(imageView, imageId);
    }

    public void setImage(Bitmap bitmap) {
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        }
    }


    public void showSuccessStatus() {
        setStatusImage(R.mipmap.icon_right);
    }

    public void showFailedStatus() {
        setStatusImage(R.mipmap.icon_wrong);
//        setImage(R.mipmap.icon_take_picture);
    }

    public interface OnAuthPictureViewClickListener {
        void onPictureViewClick(View v);
    }
}
